package com.demo.io.file;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author : YU.TAN
 * @date : 2021/11/15 15:16
 */
public class TcpServerDemo1 {
    public static void main(String[] args) throws IOException {
        //1.创建服务
        ServerSocket socket=new ServerSocket(9010);
        //2、创建监听
        Socket accept = socket.accept();
        //3、获取输入流
        InputStream inputStream = accept.getInputStream();

        //4、文件输出
        FileOutputStream fos = new FileOutputStream(new File("/main/src/resources/file/receive.jpg"));
//        FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\谭羽\\Desktop\\新建文件夹\\test.jpg"));

        byte[] bytes=new byte[1024];
        int len;
        while ((len=inputStream.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }

        //通知客户端我接收完毕了
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("您好！我接收完毕了，您可以断开了".getBytes());

        outputStream.close();
        fos.close();
        inputStream.close();
        accept.close();
        socket.close();
    }
}
